package com.game.fighting.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.texture.Texture;
import com.game.fighting.config.ConfigData;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;

/**
 * @author LeeWyatt
 */
public class ShootBarComponent extends Component {

    private SimpleDoubleProperty progress = new SimpleDoubleProperty();

    @Override
    public void onAdded() {
        Texture texture = FXGL.texture("bar.png");
        StackPane pane = new StackPane(
                FXGL.texture("bar_bg.png"), texture);

        double width = texture.getWidth();
        Rectangle rect = new Rectangle(width, texture.getHeight());
        rect.setTranslateX(-width);
        texture.setClip(rect);
        entity.getViewComponent().addChild(pane);

        progress.addListener((ob, ov, nv) ->{
            rect.setTranslateX(-width+
                    Math.min(1.0, nv.doubleValue()/ ConfigData.SHOOT_DELAY)*width);
        });
    }

    @Override
    public void onUpdate(double tpf) {
        progress.set(progress.get() + tpf);
    }

    public void reset() {
        progress.set(0);
    }
}
